package cn.iocoder.an.module.business.controller.admin.codelist;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;

public class CodeParser {

    public static ParsedCode parse(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw exception("编码格式错误!请检查");
        }


        String[] parts = code.split(";");
        if (parts.length != 3) {
            throw exception("编码格式错误!请检查");
        }

        String partNumber = removePrefix(parts[0], "P");
        String batchInfo = removePrefix(parts[1], "");
        String supplierCode = removePrefix(parts[2], "V");

        return new ParsedCode(partNumber, batchInfo, supplierCode);
    }

    private static String removePrefix(String str, String prefix) {
        return str.substring(prefix.length());
    }

    // DTO
    public static class ParsedCode {
        private final String partNumber;
        private final String batchInfo;
        private final String supplierCode;

        public ParsedCode(String partNumber, String batchInfo, String supplierCode) {
            this.partNumber = partNumber;
            this.batchInfo = batchInfo;
            this.supplierCode = supplierCode;
        }

        // Getters
        public String getPartNumber() {
            return partNumber;
        }

        public String getBatchInfo() {
            return batchInfo;
        }

        public String getSupplierCode() {
            return supplierCode;
        }

        @Override
        public String toString() {
            return "ParsedCode{" +
                    "partNumber='" + partNumber + '\'' +
                    ", batchInfo='" + batchInfo + '\'' +
                    ", supplierCode='" + supplierCode + '\'' +
                    '}';
        }
    }
}